<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/14/0014
 * Time: 16:01
 */

namespace app\sports_admin\model;


use think\Model;

class CheckOut extends Model
{
    protected $autoWriteTimestamp = true;

    protected $pk = 'id';

    protected $table = 'checkout';

    //获取教师信息
    public function teacher()
    {
        return $this->belongsTo('teacher','teacher_id','id')->field('id,teacher_name');
    }
    //获取测试类型信息
    public function sport()
    {
        return $this->belongsTo('sport','sport_id','id')->field('id,sport_name');
    }
    //获取测试类型信息
    public function getTestInfo()
    {
        return $this->belongsTo('test','test_id','id')->field('id,test_name');
    }
    //获取测试学生姓名
    public function student()
    {
        return $this->belongsTo('student','stu_id','id')->field('id,stu_name');
    }

    /*
     * 获取上课记录信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $sportName 测试名称
     * @param $teacherId 教师id
     */
    public static function getClassRecordInfo($page,$rows,$sort,$sortOrder,$teacherId,$sportId,$testId)
    {
        $result = self::with('teacher,sport,getTestInfo,student')
            ->where(function($query)use($sportId,$testId){
                if($sportId){
                    $query->where('sport_id',$sportId);
                }
                if($testId){
                    $query->where('test_id',$testId);
                }
            })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->where('teacher_id',$teacherId)->select();
        $count = self::with('teacher,sport,getTestInfo,student')
            ->where(function($query)use($sportId,$testId){
                if($sportId){
                    $query->where('sport_id',$sportId);
                }
                if($testId){
                    $query->where('test_id',$testId);
                }
        })->where('teacher_id',$teacherId)->count();
        return json(['total'=>$count,'rows'=>$result]);
    }
}